package com.juguo.charginganimation.View.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.juguo.charginganimation.Base.Baseactivty;
import com.juguo.charginganimation.Data.ApiAddress;
import com.juguo.charginganimation.Data.remote.RetrofitManager;
import com.juguo.charginganimation.R;
import com.juguo.charginganimation.utils.RxUtils;
import com.juguo.charginganimation.utils.ToastUtil;
import com.juguo.charginganimation.utils.UITools;
import com.juguo.charginganimation.repository.PixabayService;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class HelpActivity extends Baseactivty implements View.OnClickListener {

    private static final String TAG = "HelpModel";
    private CompositeDisposable mDisposable = new CompositeDisposable();

    private TextView tilite;
    private ImageView imageView;
    private EditText et_context;
    private EditText et_lxfs;
    private TextView tv_input_sum;
    private TextView tv_fs;
    private TextView tv_qq_fz;
    private TextView tv_qq;
    private TextView tv_fs_erro;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        UITools.makeStatusBarTransparent(this);
        UITools.setMIUI(this, true);
        tilite = findViewById(R.id.tv_title);
        tilite.setText("联系客服");
        inits();
        initData();
    }

    private void initData() {
        et_context.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_input_sum.setText(String.valueOf(et_context.getText().toString().trim().length()));
            }
        });
    }


    private void inits() {
        imageView = findViewById(R.id.image_back_guanyu);
        et_context = findViewById(R.id.et_context);
        et_lxfs = findViewById(R.id.et_lxfs);
        tv_input_sum = findViewById(R.id.tv_input_sum);
        tv_fs = findViewById(R.id.tv_fs);
        tv_qq_fz = findViewById(R.id.tv_qq_fz);
        tv_qq = findViewById(R.id.tv_qq);
        tv_fs_erro = findViewById(R.id.tv_fs_erro);

        et_lxfs.setOnClickListener(this::onClick);
        et_context.setOnClickListener(this::onClick);
        imageView.setOnClickListener(this::onClick);
        tv_fs.setOnClickListener(this::onClick);
        tv_qq_fz.setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_back_guanyu:
                finish();
                break;
            case R.id.tv_qq_fz:
                String content = tv_qq.getText().toString().split("：")[1];
                // qq复制
                ClipboardManager cm1 = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                cm1.setText(content);
                ToastUtil.showToast(this, "复制成功！");
                break;
            case R.id.tv_fs:
                // 发送
                String context = et_context.getText().toString().trim();
                if (TextUtils.isEmpty(context)) {
                    ToastUtil.showLongToast(this, "请输入您的问题和意见？");
                } else {
                    httpGetHelp();
                    break;
                }
        }
    }

    private void httpGetHelp() {
        String context = et_context.getText().toString().trim();
        String contact = et_lxfs.getText().toString();
        PixabayService service = RetrofitManager.getInstance()
                .getRetrofit()
                .create(PixabayService.class);
        Map<String, Object> map = new HashMap<>();
        map.put("contact", contact);
        map.put("content", context);
        map.put("appId", ApiAddress.WX_APP_ID);
        Map<String, Object> param = new HashMap<>();
        param.put("param", map);
        RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), new Gson().toJson(param));
        mDisposable.add(service.getfeedBack(body)
                .compose(RxUtils.schedulersTransformer())
                .subscribe(helpResponseBean -> {
                    Log.d(TAG, "loadMore: " + helpResponseBean);
                    ToastUtil.showToast(getApplication(), "提交成功！");
                    finish();
                }, throwable -> {
                    Log.d(TAG, "loadMore: " + throwable);
                    tv_fs_erro.setVisibility(View.VISIBLE);
                    ToastUtil.showToast(getApplication(), getResources().getString(R.string.erro));
                }));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}